import axios, {AxiosInstance} from "axios";
const isDev = process.env.NODE_ENV === 'development';
const myAxios:AxiosInstance = axios.create({
    baseURL: isDev ? 'http://localhost:8080/api' : 'http://47.116.188.71/api',
    withCredentials: true,
});
myAxios.defaults.withCredentials = true;//表示向后端发送请求时，需要携带上cookie


myAxios.interceptors.request.use(function (config){
    console.log('我要发请求了',config);
    return config;
},function (error) {
    return Promise.reject(error);
})

myAxios.interceptors.response.use(function (response) {
    console.log('我收到你的响应啦', response)
    // 未登录则跳转到登录页
    if (response?.data?.code === 40101) {
        const redirectUrl = window.location.href;
        window.location.href = `/user/login?redirect=${redirectUrl}`;
    }
    // Do something with response data
    return response.data;
}, function (error) {
    // Do something with response error
    return Promise.reject(error);
});
export default myAxios;